package com.lzy.huijinjiazu.mvp.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerMyPointsComponent;
import com.lzy.huijinjiazu.di.module.MyPointsModule;
import com.lzy.huijinjiazu.mvp.contract.MyPointsContract;
import com.lzy.huijinjiazu.mvp.presenter.MyPointsPresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.BalaceDetailedAdapter;
import com.lzy.huijinjiazu.mvp.ui.adapter.GoodsAdapter;
import com.lzy.huijinjiazu.mvp.ui.listener.ScrollViewListener;
import com.lzy.huijinjiazu.mvp.ui.widget.SlideScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MyPointsActivity extends BaseActivity<MyPointsPresenter> implements MyPointsContract.View, ScrollViewListener {


    @BindView(R.id.rv_detailed)
    RecyclerView mRvDetailed;
    @BindView(R.id.scrollView)
    SlideScrollView mScrollView;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.rv_goods)
    RecyclerView mRvGoods;
    private BalaceDetailedAdapter mDetailedAdapter;
    private GoodsAdapter mGoodsAdapter;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerMyPointsComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myPointsModule(new MyPointsModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_my_points; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mScrollView.setScrollViewListener(this);
        initTitle();
        initRecyclerView();
        detailedData();
        goodsData();
    }

    private void initTitle() {
        mFlTitle.setBackgroundResource(R.mipmap.bg_theme_title);
        mFlTitle.getBackground().setAlpha(0);
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvDetailed, new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mDetailedAdapter = new BalaceDetailedAdapter();
        mRvDetailed.setAdapter(mDetailedAdapter);
        ArmsUtils.configRecyclerView(mRvGoods, new GridLayoutManager(this, 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvGoods.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                outRect.right = (itemPosition + 1) % 2 == 0 ? 0 : ArmsUtils.dip2px(MyPointsActivity.this, 2.5f);
                outRect.left = (itemPosition + 1) % 2 == 0 ? ArmsUtils.dip2px(MyPointsActivity.this, 2.5f) : 0;
                outRect.bottom = ArmsUtils.dip2px(MyPointsActivity.this, 5);
            }
        });
        mGoodsAdapter = new GoodsAdapter();
        mRvGoods.setAdapter(mGoodsAdapter);
    }


    private void detailedData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            data.add("");
        }
        mDetailedAdapter.setNewData(data);
    }

    private void goodsData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            data.add("");
        }
        mGoodsAdapter.setNewData(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBar(this, mFlTitle);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onScrollChanged(SlideScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (mFlTitle==null){
            return;
        }
        if (y > 350) {
            mFlTitle.getBackground().setAlpha(255);
            return;
        }
        if (y <= 250 && y > 0) {
            mFlTitle.getBackground().setAlpha(y);
        }
    }

}
